/*
 * @Author: zhengzhuang
 * @Date: 2020-08-24 15:49:39
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-02-19 14:33:11
 * @Description: 城际班次乘客 FlightPassengers
 * @FilePath: /C20Driver/lib/pages/intercity_car/flight_passengers.dart
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/model/get_peer_orders_by_time_table.dart';
import 'package:C20Driver/model/get_trip_route_data.dart';
import 'package:C20Driver/provider/driver_information.dart';
import 'package:C20Driver/provider/order_information.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/widget/no_data_widget.dart';
import 'package:C20Driver/widget/spinkit.dart';
import 'package:provider/provider.dart';

class FlightPassengers extends StatefulWidget {
  final String id;
  FlightPassengers({this.id});
  @override
  _FlightPassengersState createState() => _FlightPassengersState();
}

class _FlightPassengersState extends State<FlightPassengers> {
  List _orderList;
  bool _updataState = true;

  GetTripRouteData _dataInfo;

  Future _getIntercityShift() async {
    return await Request.getPeerOrdersByTimeTableID(widget.id)
        .then((value) async {
      return value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('查看乘客'),
      ),
      body: Container(
        child: RefreshIndicator(
          onRefresh: () async {
            setState(() {
              _updataState = true;
            });
          },
          child: FutureBuilder(
              future: _updataState ? _getIntercityShift() : '',
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  List _infoData = snapshot.data;
                  if (_infoData.length == 0) return NoDataWidget();
                  return ListView(
                    children:
                        _infoData.map((e) => _orderListContent(e)).toList(),
                  );
                } else {
                  return Spinkit();
                }
              }),
        ),
      ),
    );
  }

  /// 顶部派单内容
  Widget _topOrderContent(GetTripRouteData dataInfo) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(30.0),
          right: ScreenUtil().setWidth(30.0),
          top: ScreenUtil().setWidth(10.0),
          bottom: ScreenUtil().setWidth(10.0)),
      decoration: BoxDecoration(color: Colors.white, boxShadow: <BoxShadow>[
        BoxShadow(color: Colors.black26, blurRadius: ScreenUtil().setSp(20.0)),
      ]),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          Text(
            '城际指派订单',
            style: TextStyle(
                fontSize: ScreenUtil().setSp(28.0),
                fontWeight: FontWeight.w700),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                width: ScreenUtil().setWidth(490.0),
                height: ScreenUtil().setWidth(70.0),
                decoration: BoxDecoration(
                    color: Color.fromARGB(255, 230, 230, 234),
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(5.0))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        _blueDots(),
                        SizedBox(width: ScreenUtil().setWidth(10.0)),
                        Container(
                            width: ScreenUtil().setWidth(150.0),
                            child: Text('${dataInfo.routeData.orginPoint}',
                                maxLines: 1,
                                style: TextStyle(
                                    fontSize: ScreenUtil().setSp(34.0),
                                    color: Colors.black87,
                                    fontWeight: FontWeight.w400))),
                      ],
                    ),
                    Icon(Icons.trending_flat,
                        color: Colors.black, size: ScreenUtil().setSp(60.0)),
                    Row(
                      children: <Widget>[
                        _orangeDots(),
                        SizedBox(width: ScreenUtil().setWidth(10.0)),
                        Container(
                            width: ScreenUtil().setWidth(150.0),
                            child: Text('${dataInfo.routeData.endPoint}',
                                maxLines: 1,
                                style: TextStyle(
                                    fontSize: ScreenUtil().setSp(34.0),
                                    color: Colors.black87,
                                    fontWeight: FontWeight.w400))),
                      ],
                    ),
                  ],
                ),
              ),
              MaterialButton(
                color: Color.fromARGB(255, 78, 79, 95),
                minWidth: ScreenUtil().setWidth(150.0),
                height: ScreenUtil().setWidth(70.0),
                child: Text('补票收款',
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(26.0),
                        color: Colors.white)),
                onPressed: () => NavigatorUtil.jump(context,
                    '/setCollectionAmount?orginPoint=${Uri.encodeComponent(dataInfo.routeData.orginPoint)}&endPoint=${Uri.encodeComponent(dataInfo.routeData.endPoint)}&unitPrice=${dataInfo.routeData.unitPrice}'),
              ),
            ],
          ),
          Text(
            '12月10日 ${dataInfo.schedule.pickUpTime}出发 可乘坐${dataInfo.schedule.seatNum}人  余坐：${dataInfo.schedule.surplusNumber}人',
            style: TextStyle(
                fontSize: ScreenUtil().setSp(26.0),
                color: Color.fromARGB(255, 250, 144, 0)),
          ),
        ],
      ),
    );
  }

  /// 列表内容
  Widget _orderListContent(dataList) {
    GetPeerOrdersByTimeTable modelData =
        GetPeerOrdersByTimeTable.fromJson(dataList);
    return InkWell(
      child: Container(
        margin: EdgeInsets.only(
            left: ScreenUtil().setWidth(30.0),
            right: ScreenUtil().setWidth(30.0),
            top: ScreenUtil().setWidth(10.0),
            bottom: ScreenUtil().setWidth(5.0)),
        padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(30.0),
            right: ScreenUtil().setWidth(30.0),
            top: ScreenUtil().setWidth(30.0),
            bottom: ScreenUtil().setWidth(30.0)),
        width: ScreenUtil().setWidth(690.0),
        height: ScreenUtil().setWidth(300.0),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10.0)),
            boxShadow: <BoxShadow>[
              BoxShadow(
                  color: Colors.black26, blurRadius: ScreenUtil().setSp(20.0)),
            ]),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Container(
                      width: ScreenUtil().setWidth(100.0),
                      height: ScreenUtil().setWidth(100.0),
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        image: DecorationImage(
                            image: NetworkImage(
                          '${modelData.headimgurl}',
                        )),
                      ),
                    ),
                    SizedBox(width: ScreenUtil().setWidth(20.0)),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Text(
                              '尾号${modelData.lastNum}',
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(32.0),
                                  color: Colors.black),
                            ),
                            SizedBox(width: ScreenUtil().setWidth(20.0)),
                            Offstage(
                              offstage: modelData.state != '0020',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 255, 162, 96),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('已派单',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                            SizedBox(width: ScreenUtil().setWidth(20.0)),
                            Offstage(
                              offstage: modelData.state != '0040',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 255, 162, 96),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('待搭乘',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                            Offstage(
                              offstage: modelData.state != '0045',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 255, 162, 96),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('接人中',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                            Offstage(
                              offstage: modelData.state != '0050',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 255, 162, 96),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('待搭乘',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                            Offstage(
                              offstage: modelData.state != '0055',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 255, 162, 96),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('上车点',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                            Offstage(
                              offstage: modelData.state != '0060',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 144, 148, 182),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('已上车',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                            Offstage(
                              offstage: modelData.state != '0065',
                              child: Container(
                                height: ScreenUtil().setWidth(43.0),
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10.0),
                                    right: ScreenUtil().setWidth(10.0)),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 144, 148, 182),
                                    borderRadius: BorderRadius.circular(
                                        ScreenUtil().setWidth(6.0))),
                                child: Text('到达目的地',
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(28.0),
                                        color: Colors.white)),
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: ScreenUtil().setWidth(10.0)),
                        Text(
                          '${modelData.departureDate} ${modelData.week} ${modelData.departureTime}  ${modelData.peopleNumber}人',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(28.0),
                              color: Colors.orange,
                              fontWeight: FontWeight.w700),
                        )
                      ],
                    ),
                  ],
                ),
                // Container(
                //   width: ScreenUtil().setWidth(140.0),
                //   alignment: Alignment.centerRight,
                //   child: RichText(
                //     overflow: TextOverflow.clip,
                //     maxLines: 1,
                //     text: TextSpan(
                //         text: '¥',
                //         style: TextStyle(
                //             fontSize: ScreenUtil().setSp(30.0),
                //             color: Colors.black,
                //             fontWeight: FontWeight.w700),
                //         children: <TextSpan>[
                //           TextSpan(
                //               text: '${modelData.totalPrice}',
                //               style: TextStyle(
                //                   fontSize: ScreenUtil().setSp(60.0),
                //                   color: Colors.black,
                //                   fontWeight: FontWeight.w400))
                //         ]),
                //   ),
                // ),
              ],
            ),
            Divider(
                height: ScreenUtil().setWidth(1.0),
                color: Color.fromARGB(255, 228, 228, 228)),
            Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    SizedBox(width: ScreenUtil().setWidth(10.0)),
                    _blueDots(),
                    SizedBox(width: ScreenUtil().setWidth(20.0)),
                    Container(
                      width: ScreenUtil().setWidth(570.0),
                      child: Text('${modelData.depAre}',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30.0),
                            color: Colors.black,
                          ),
                          maxLines: 1),
                    ),
                  ],
                ),
                SizedBox(height: ScreenUtil().setWidth(10.0)),
                Row(
                  children: <Widget>[
                    SizedBox(width: ScreenUtil().setWidth(10.0)),
                    _orangeDots(),
                    SizedBox(width: ScreenUtil().setWidth(20.0)),
                    Container(
                      width: ScreenUtil().setWidth(570.0),
                      child: Text('${modelData.destArea}',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30.0),
                            color: Colors.black,
                          ),
                          maxLines: 1),
                    ),
                  ],
                )
              ],
            ),
          ],
        ),
      ),
      onTap: () {
        DriverInformation provider =
            Provider.of<DriverInformation>(context, listen: false);
        // if (provider.driverInfo.workingStatus == '0020' || provider.driverInfo.workingStatus == '0040') {
        Provider.of<OrderInformation>(context, listen: false)
            .setOrderInfo(orderId: modelData.orderId);
        switch (modelData.state) {
          case '0040':
            // 去接乘客
            NavigatorUtil.jump(context,
                    '/intercityPickUpPassengers?orderId=${modelData.id}')
                .then((value) {
              setState(() {
                _updataState = true;
              });
            });
            break;
          case '0045':
            // 到达乘客约定地点
            NavigatorUtil.jump(context,
                    '/intercityPickUpPassengers?orderId=${modelData.id}')
                .then((value) {
              setState(() {
                _updataState = true;
              });
            });
            break;
          case '0055':
            // 等待乘客上车
            NavigatorUtil.jump(context,
                    '/intercityPickUpPassengers?orderId=${modelData.id}')
                .then((value) {
              setState(() {
                _updataState = true;
              });
            });
            break;
          case '0060':
            // 到达目的地
            NavigatorUtil.jump(context,
                    '/intercityPickUpPassengers?orderId=${modelData.id}')
                .then((value) {
              setState(() {
                _updataState = true;
              });
            });
            break;
          default:
        }
        // } else {
        // eventBus.fire(MyEvent('0'));
        // ToastX.center('您还未出车');
        // }
      },
    );
  }

  /// 蓝色圆点
  Widget _blueDots() {
    return Container(
      width: ScreenUtil().setWidth(13.0),
      height: ScreenUtil().setWidth(13.0),
      decoration: BoxDecoration(color: Colors.blue, shape: BoxShape.circle),
    );
  }

  /// 橙色圆点
  Widget _orangeDots() {
    return Container(
      width: ScreenUtil().setWidth(13.0),
      height: ScreenUtil().setWidth(13.0),
      decoration: BoxDecoration(color: Colors.orange, shape: BoxShape.circle),
    );
  }

  /// 正在指派订单
  Widget _assigningOrder() {
    return Container(
      // height: double.infinity,
      width: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Image.asset(
            'assets/icon/assigningOrderIcon.png',
            width: ScreenUtil().setWidth(400.0),
            height: ScreenUtil().setWidth(400.0),
          ),
          Text('今日未有乘客',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(28.0),
                  color: Color.fromARGB(255, 187, 189, 223))),
          // Text('请稍后...',
          //     style: TextStyle(
          //         fontSize: ScreenUtil().setSp(28.0),
          //         color: Color.fromARGB(255, 187, 189, 223))),
        ],
      ),
    );
  }
}
